﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spun.Bindings;
using Spun.Utility;
using Spun.Naming.Paths;
using System.Threading;
using Spun.Utility.Timing;

namespace Spun.Polling
{
    public class PollingPlace : IFactory<IBinding, Path>, IDisposable
    {
        public PollingPlace()
        {
            Sync = new object();
            Updates = new List<Update>();
            TimeSpanBinder = new TimeSpanBinder();
            TimeSpanBinder.Start();
        }

        public void StartPoll(TimeSpan Timeout, IPoller Poller)
        {
            EndOfOperationDelegate CompletedDelegate = delegate()
            {
                lock (Sync)
                {
                    Poller.OnUpdatesBecameAvailable(Updates);
                    Updates = new List<Update>();
                    TimeBoundOperation = null;
                }
            };

            EndOfOperationDelegate TimeoutDelegate = delegate()
            {
                lock (Sync)
                {
                    Poller.OnTimeout();
                    TimeBoundOperation = null;
                }
            };

            lock (Sync)
            {
                if (Updates.Count > 0)
                {
                    CompletedDelegate();
                }
                else
                {
                    TimeBoundOperation = new TimeBoundOperation(Timeout);

                    TimeBoundOperation.OnCompleted += CompletedDelegate;
                    TimeBoundOperation.OnTimeout += TimeoutDelegate;

                    TimeSpanBinder.BindOperation(TimeBoundOperation);
                }
            }
        }

        public void OnBindingUpdate(Path Path, bool Valid, object Object)
        {
            var Update = new Update
            {
                Path = Path,
                Object = Object,
                Valid = Valid
            };

            lock (Sync)
            {
                Updates.Add(Update);

                if (TimeBoundOperation != null)
                {
                    TimeBoundOperation.Complete();
                    TimeBoundOperation = null;
                }
            }
        }

        #region IFactory<IBinding, Path> Members

        public IBinding New(Path Path)
        {
            return new Binding(this, Path);
        }

        #endregion

        private List<Update> Updates;
        private object Sync;
        private TimeBoundOperation TimeBoundOperation;
        private TimeSpanBinder TimeSpanBinder;

        #region IDisposable Members

        public void Dispose()
        {
            TimeSpanBinder.SignalToStop();
            TimeSpanBinder.Join();
        }

        #endregion
    }
}
